package xcmg.device.web.wechat;

import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.CompanyConstants;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.annotation.ExternalApiRequest;
import xcmg.device.infra.interceptor.externalApiCheck.handle.ApiCheckStrategy.ApiCheckOptionEnum;
import xcmg.device.service.vo.wechat.DealerStorefrontVO;
import xcmg.device.service.vo.wechat.StoreSearchParam;
import xcmg.device.service.wechat.StorefrontService;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

@RestController
@RequestMapping("/storefront")
public class StorefrontController extends BasicController {
    @Autowired
    private StorefrontService storefrontService;

    @RequestMapping(value = "/addStore",method = RequestMethod.POST)
    @ApiOperation(value = "新增网点信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.Storefront.ADD)
    public ActionResult addStore(@RequestBody DealerStorefrontVO dealerStorefrontVO) throws Exception {
        if (null == dealerStorefrontVO.getOrgId()) {
            dealerStorefrontVO.setOrgId(getOrgId());
        }
        if (null == dealerStorefrontVO.getCreateId()) {
            String userId = getUserId();
            dealerStorefrontVO.setCreateId(userId);
            dealerStorefrontVO.setUpdateId(userId);
        }
        ErrorCode code=storefrontService.addStore(dealerStorefrontVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "编辑网点信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.Storefront.EDIT)
    public ActionResult update(@RequestBody DealerStorefrontVO dealerStorefrontVO) throws Exception {
        dealerStorefrontVO.setUpdateId(getUserId());
        ErrorCode code=storefrontService.update(dealerStorefrontVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除网点信息")
    @FastMappingInfo(needLogin = true,code = AuthCode.Storefront.DEL)
    public ActionResult delete(@RequestBody DealerStorefrontVO dealerStorefrontVO) throws Exception {
        ErrorCode code=storefrontService.delete(dealerStorefrontVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation("网点列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<StoreSearchParam> condition)throws Exception{
        StoreSearchParam storeSearchParam =condition.getFilters();
        if(storeSearchParam == null){
            storeSearchParam = new StoreSearchParam();
        }
        storeSearchParam.setOrgId(getOrgId());
        condition.setFilters(storeSearchParam);
        PageCommonVO pageCommonVO= storefrontService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @ApiOperation("网点详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String storeId){
        return actionValueResult(storefrontService.item(storeId));
    }

    @RequestMapping(value = "/listCy4Sync", method = RequestMethod.GET)
    @ApiOperation("铲运代理商网点信息同步接口")
    @ExternalApiRequest(ApiCheckOptionEnum.OPTION_SECRET)
    public ActionResult listCy4Sync() {
        List<DealerStorefrontVO> result;
        try {
            result = storefrontService.list4Sync(CompanyConstants.CY_ORG_ID);
        } catch (Exception e) {
            return new ActionResult(yb.ecp.fast.infra.constants.ErrorCode.Failure.getCode(),
                e.getMessage());
        }
        return actionValueResult(result);
    }

}

